package com.googlecode.adaboost.trainer;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;

import com.googlecode.adaboost.exception.AdaBoostException;

/**
 * @version Create on 2012-11-11
 * @author Yinzi Chen
 */

public class TrainingData {

	private int dataNum;
	private int inputNum;
	private Vector<DataElement> trainingData;

	public TrainingData() {
		trainingData = new Vector<DataElement>();
	}

	public void load(String fileName) throws AdaBoostException,
			FileNotFoundException {
		Scanner cin = null;
		try {
			cin = new Scanner(new File(fileName));
			loadData(cin);
		} finally {
			cin.close();
		}
	}

	private void loadData(Scanner cin) throws AdaBoostException {
		trainingData.clear();
		dataNum = cin.nextInt();
		inputNum = cin.nextInt();
		DataElement[] data = new DataElement[dataNum];
		for (int i = 0; i < dataNum; ++i) {
			Vector<Double> input = new Vector<Double>();
			for (int j = 0; j < inputNum; ++j) {
				input.add(cin.nextDouble());
			}
			int output = cin.nextInt();
			if (output != -1 && output != 1) {
				throw new AdaBoostException(
						"Output value invalid, can only be 1/-1, value: "
								+ output);
			}
			data[i] = new DataElement(input, output);
		}
		Arrays.sort(data);
		for (int i = 0; i < dataNum; ++i) {
			trainingData.add(data[i]);
		}
	}

	public Vector<DataElement> getTrainingData() {
		return trainingData;
	}

	public void setTrainingData(Vector<DataElement> trainingData) {
		this.trainingData = trainingData;
	}

	public int getDataNum() {
		return dataNum;
	}

	public void setDataNum(int dataNum) {
		this.dataNum = dataNum;
	}

}
